<?php

namespace app\common\model;

use app\common\enum\Block as EnumBlock;
use app\common\model\BaseModel;

/**
 * 内容块模型 
 * Class Block
 * @package app\common\model
 */
class Block extends BaseModel
{
    protected $name = 'free_block';
    protected $pk = 'free_block_id';
    protected $append = ['type'];


    /**
     * 返回自由块类型
     * @return \app\common\enum\Block
     */
    public function getCategory(){
       return EnumBlock::data();
    }

    /**
     * 关联内容块分类
     * @return \app\common\enum\Block
     */
    public function getTypeAttr($value,$data)
    {
        return EnumBlock::data()[$data['block_type']]['name'];
    }

    
    /**
     * 处理内容块图集
     * @return \app\common\model\UploadFile
     */
    public function getContentAttr($value,$data)
    {
        if($data['block_type'] == 10)
        {
            return UploadFile::where('file_id','=',$data['content'])->find()['file_path'];
        }elseif($data['block_type'] == 20)
        {
            return UploadFile::whereIN('file_id',$data['content'])->select();
        }
        return $value;
    }

    /**
     * 内容块详情
     * @param $block_id
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($block_id)
    {
        return self::get($block_id);
    }

}
